﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using eSYSU.DAL;
using eSYSU.Model;


namespace eSYSU.Business
{
    /// <summary>
    /// 新闻评论服务业务类
    /// </summary>
    public class NewsCommentService
    {
        public NewsCommentService()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }


        public static DataTable GetCommentsByNewsID(int newsID)
        {
            IDictionary<string, object> parms = new Dictionary<string, object>();
            parms.Add("@NewsID", newsID);
            parms.Add("@UserName", null);
            parms.Add("@CommentID", null);

            return SqlHelper.GetDataTable(SqlHelper.ConnString, CommandType.StoredProcedure, "proc_Comment_Get", parms);
        }


        public static NewsComment GetCommentByID(int CommentID)
        {
            IDictionary<string, object> parms = new Dictionary<string, object>();
            parms.Add("@CommentID", CommentID);
            parms.Add("@UserName", null);
            parms.Add("@NewsID", null);

            DataTable dt = SqlHelper.GetDataTable(SqlHelper.ConnString, CommandType.StoredProcedure, "proc_Comment_Get", parms);

            return null;
        }

        /// <summary>
        /// 根据新闻ID统计评论总数
        /// </summary>
        /// <param name="newsID">新闻ID</param>
        /// <returns>返回该新闻的评论总数</returns>
        public static int GetCommentCount(int newsID)
        {
            IDictionary<string, object> parms = new Dictionary<string, object>();
            parms.Add("@NewsID", newsID);
            parms.Add("@UserName", null);

            return Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnString, CommandType.StoredProcedure, "proc_Comment_Count", parms));
        }


        /// <summary>
        /// 根据用户名统计该用户的评论总数
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>返回该用户的评论总数</returns>
        public static int GetCommentCount(string username)
        {
            IDictionary<string, object> parms = new Dictionary<string, object>();
            parms.Add("@UserName", username);
            parms.Add("@NewsID", null);

            return Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnString, CommandType.StoredProcedure, "proc_Comment_Count", parms));
        }


        public static DataTable GetCommentByUsername(string username)
        {
            IDictionary<string, object> parms = new Dictionary<string, object>();
            parms.Add("@UserName", username);
            parms.Add("@NewsID", null);
            parms.Add("@CommentID", null);

            return SqlHelper.GetDataTable(SqlHelper.ConnString, CommandType.StoredProcedure, "proc_Comment_Get", parms);
        }


        public static DataTable GetHotCommentNews(int newsCount)
        {
            IDictionary<string, object> parms = new Dictionary<string, object>();
            parms.Add("@newsCount", newsCount);

            return SqlHelper.GetDataTable(SqlHelper.ConnString, CommandType.StoredProcedure, "proc_News_Hot_Comment", parms);
        }


        /// <summary>
        /// 插入一条新闻评论
        /// </summary>
        /// <param name="comment">评论</param>
        /// <returns></returns>
        public static object InserNewsComment(NewsComment comment)
        {
            IDictionary<string, object> parmsDic = new Dictionary<string, object>();
            parmsDic.Add("@newsid", comment.newsid);
            parmsDic.Add("@username", comment.username);
            parmsDic.Add("@posttime", comment.posttime);
            parmsDic.Add("@content", comment.content);

            object obj = SqlHelper.ExecuteScalar(SqlHelper.ConnString, CommandType.StoredProcedure, "proc_Comment_Insert", parmsDic);
            return obj;
        }


        /// <summary>
        /// 将XML字符串格式化为NewsComment对象
        /// </summary>
        /// <param name="xmlComment">XML字符串</param>
        /// <returns>NewsComment对象</returns>
        public static NewsComment LoadXml(string xmlComment)
        {
            NewsComment comment = new NewsComment();

            System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
            xd.LoadXml(xmlComment);

            comment.id = Convert.ToInt32(xd.SelectSingleNode("//ID").InnerText);
            comment.newsid = Convert.ToInt32(xd.SelectSingleNode("//NewsID").InnerText);
            comment.username = xd.SelectSingleNode("//Username").InnerText;
            comment.posttime = DateTime.Now;
            comment.content = xd.SelectSingleNode("//Content").InnerText;

            return comment;
        }


    }
}